<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>getBattery电池的信息</title>
  <!-- 提供有关计算机电池的信息。它显示电池的剩余容量和充电状态。它还评估了电池完全充电或放电所需的时间 -->
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  "use strict"; {
    navigator.getBattery().then(battery => {
      //是否在充电
      battery.addEventListener("chargingchange", () => {
        alert(battery.charging ? '在充电' : '没在充电')
      })

      //电池剩余时间改变时触发该事件
      battery.addEventListener("dischargingtimechange", res => {
        console.log("电池剩余时间改变时触发该事件", res)
      })

      //在电池电量水平改变时触发
      battery.addEventListener("levelchange", res => {
        console.log("在电池电量水平改变时触发", res)
      })
    })
  };
</script>

</html>